import Vue from 'vue'
import moment from 'moment'
const state = {
  testData: [],
  meetRoomData: []
}
const getters = {

}
const mutations = {
  SET_MEETDATA(state) {
  },
  // 会议室列表
  GET_MEETROOMDATA(state, payload) {
    var tempArr = payload.data.map((val) => {
      return {
        address: val.addr,
        name: val.name,
        numPeople: val.peopleNum,
        roomId: val.id,
        operat: ''
      }
    })
    state.meetRoomData = tempArr
  },
  // 会议列表
  GET_MEETLISTDATA(state, payload) {
    console.log('12345678', payload)
    var middleList = payload.data.map((val) => {
      return {
        mtitle: val.title,
        title: moment(val.startDate * 1000).format('HH:mm') + val.title,
        // title: val.title,
        address: val.addr,
        name: val.name,
        applyPeople: val.proposer,
        meetType: val.type,
        joinNum: val.number,
        meetContent: val.content,
        start: moment(val.startDate * 1000).format('YYYY-MM-DD HH:mm'),
        end: moment(val.endDate * 1000).format('YYYY-MM-DD HH:mm'),
        roomId: val.pid,
        meetId: val.id,
        operat: ''
      }
    })
    state.testData = middleList
  }
}

const actions = {
  // 创建会议
  createMetting({ commit, state }, payload) {
    return Vue.http.post('/conference', {
      pid: payload.pid,
      title: payload.title,
      type: payload.type,
      proposer: payload.proposer,
      number: payload.number,
      startDate: payload.startDate,
      endDate: payload.endDate,
      content: payload.content
    }).then((res) => {
      console.log('/conference', res)
      commit('SET_MEETDATA', res)
    }).catch(err => {
      console.log('post /conference:' + err)
    })
  },
  // 修改会议
  changeMetting({ commit, state }, payload) {
    return Vue.http.put('/conference/' + payload.id, {
      id: payload.id,
      pid: payload.pid,
      title: payload.title,
      type: payload.type,
      proposer: payload.proposer,
      number: payload.number,
      startDate: payload.startDate,
      endDate: payload.endDate,
      content: payload.content
    }).then((res) => {
      console.log('/conference/:id', res)
      // commit('SET_MEETDATA', res)
    }).catch(err => {
      console.log('post /conference:' + err)
    })
  },
  // 删除会议
  deleteMetting({ commit, state }, payload) {
    console.log('payload', payload)
    return Vue.http.delete('/conference/' + payload.id, { id: payload.id }).then((res) => {
      console.log('/conference/:id', res)
      // commit('SET_MEETROOMDATA', res)
    }).catch(err => {
      console.log('DELETE /conference/:id' + err)
    })
  },
  // 获取会议列表
  createMeetList({ commit, state }, payload) {
    return Vue.http.get('/conference').then((res) => {
      console.log('/conference', res.data)
      commit('GET_MEETLISTDATA', res.data)
    }).catch(err => {
      console.log('post /conference:' + err)
    })
  },
  // 添加会议室
  createMettRoom({ commit, state }, payload) {
    console.log(payload)
    if (payload) {
      return Vue.http.post('/conferenceRoom', {
        addr: payload.addr,
        name: payload.name,
        peopleNum: payload.peopleNum
      }).then((res) => {
        console.log('/conferenceRoom', res)
        commit('SET_MEETDATA', res)
      }).catch(err => {
        console.log('post /conferenceRoom:' + err)
      })
    } else { // 获取会议列表
      return Vue.http.get('/conferenceRoom').then((res) => {
        console.log('/conferenceRoom', res.data)
        commit('GET_MEETROOMDATA', res.data)
      }).catch(err => {
        console.log('post /conferenceRoom:' + err)
      })
    }
  },
  // 删除会议室列表
  deleteRoomData({ commit, state }, payload) {
    console.log(payload)
    return Vue.http.delete('/conferenceRoom/' + payload.id, { id: payload.id }).then((res) => {
      console.log('/conferenceRoom/:id', res)
      // commit('SET_MEETROOMDATA', res)
    }).catch(err => {
      console.log('DELETE /conferenceRoom/:id' + err)
    })
  },
  // 修改会议室
  changeMettRoom({ commit, state }, payload) {
    console.log(payload)
    return Vue.http.put('/conferenceRoom/' + payload.id, {
      id: payload.id,
      addr: payload.addr,
      name: payload.name,
      peopleNum: payload.peopleNum
    }).then((res) => {
      console.log('/conferenceRoom/:id', res)
      // commit('SET_MEETROOMDATA', res)
    }).catch(err => {
      console.log('DELETE /conferenceRoom/:id' + err)
    })
  },
}

export default {
  state,
  mutations,
  actions,
  getters
}
